function dirname(path) {
    var pathset = [];
    $.string(path).scan(/[^/]+/, function (m) { pathset.push(m) });
    pathset = pathset.slice(0, pathset.length - 1);
    result = pathset.join('/');
    return '/' + result + '/';
}

function basename(path) {
    var pathset = [];
    $.string(path).scan(/[^/]+/, function (m) { pathset.push(m) });
    pathset = pathset.slice(pathset.length - 1, pathset.length);
    result = pathset.join('/');
    return result;
}

function setMsg(msg){
    $('#msg').text(msg);
    $('#msg').fadeIn('slow', function() {});
    $('#msg').fadeOut('slow');
    $('#msg').fadeIn('slow', function() {});
    $('#msg').fadeOut('slow');
    $('#msg').fadeIn('slow', function() {});
    $('#msg').fadeOut('slow');
}

function login(username, password) {
    $.post(
	'/login.html',
	'username=' + username + '&password=' + password,
	function(data) {
	    if (data != 'Login fail.') {
		window.location.href = data;
		$('#login-dialog').dialog('destroy');
	    }
	    else {
		setMsg(data);
	    }
	});
}

function back2root() {
    window.location.href = '/';
}

function logout() {
    $.post('/logout.html', back2root);
}

function createGroup(name) {
    $.post('/?qqfile=' + name + '&path=/groups/', ' ', setMsg);
    loadGroupList();
}

function register(username, password, confirm) {
    if (password == confirm) {
	$.post('/register.html',
	       'username=' + username + '&password=' + password,
	       setMsg);
	$('#register-dialog').dialog('destroy');
    }
}

function mkcol(parent, dirname){
    $.get(parent + dirname + '?action=mkcol', refleshFileList);
}

function refleshFileList(data) {
    setMsg(data);
    loadFileList(current_path);
}

function refleshGroupList(data) {
    setMsg(data);
    loadGroupList();
}

function deleteIt(path){
    $.get(path + '?action=delete', refleshFileList);
}

function renameToggle(name){
    $('a[name="' + name + '"]').toggle();
    $('input[name="' + name + '"]').toggle();
}

function renameIt(href, newname){
    $.get(href + '?action=move&dst=' + dirname(href) + newname, refleshFileList);
}

function moveIt(src, dst) {
    $.get(src + '?action=move&dst=' + dst);
    loadFileList(current_path);
}

function grantIt(path, principal, privilege, isgrant) {
    if (isgrant) {
	$.get(path + '?action=grant&add&principal=' + principal + '&privilege=' + privilege, refleshFileList);
    }
    else {
	$.get(path + '?action=grant&remove&principal=' + principal + '&privilege=' + privilege, refleshFileList);
    }
}

function joinGroup(path, principal, isjoin) {
    if (isjoin) {
	$.get(path + '?action=join-group&join&principal=' + principal, refleshGroupList);
    }
    else {
	$.get(path + '?action=join-group&exit&principal=' + principal, refleshGroupList);
    }
}

function startSearching() {
    $('#search-result').empty();
    $('#search-result').append('<div class="loading"></div>')
}

function finishSearching() {
    $('#search-result .loading').remove();
}

function search(path, type, kvdict, container) {
    path += '?action=search&type=' + type + '&';
    for(var key in kvdict) {
	path += key + '=' + kvdict[key] + '&';
    }
    startSearching();
    $(container).load(path, finishSearching);
}

function modifyProperty(path, name, value) {
    $.get(path + name + '?action=propertyupdate&' + name + '=' + value, refleshFileList);
}

